﻿using System;

namespace DAPNet
{
    public class PeakNormalizer : IOfflineEffect
    {
        private double parameter;


        public PeakNormalizer(double parameter)
        {
            this.parameter = parameter;
        }

        public double Parameter
        {
            get
            {
                return parameter;
            }
            set
            {
                parameter = value;
            }
        }

        public void Process(SampleCollection samples)
        {
            double maxAbsValue = GetMaxAbsValue(samples);
            double ratio = Math.Abs(parameter) / maxAbsValue;
            for (int i = 0; i < samples.Count; i++)
            {
                double normalizedValue = samples[i].Value * ratio;
                samples[i].SetTrimmedValue(normalizedValue);
            }
        }

        private double GetMaxAbsValue(SampleCollection samples)
        {
            double maxAbsValue = Sample.BaseValue;
            for (int i = 0; i < samples.Count; i++)
            {
                maxAbsValue = Math.Max(maxAbsValue, Math.Abs(samples[i].Value));
            }
            return maxAbsValue;
        }
    }
}
